package com.neusoft.mapper;

import com.neusoft.po.Points;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

@Mapper
public interface PointsMapper {

    // 查询用户的可用积分
    @Select("SELECT * FROM Points WHERE userId = #{userId} AND valid = 1 AND date <= #{currentDate}")
    List<Points> getAvailablePoints(@Param("userId") String userId, @Param("currentDate") Date currentDate);

    // 查询用户的过期积分
    @Select("SELECT * FROM Points WHERE userId = #{userId} AND valid = 1 AND date > #{currentDate}")
    List<Points> getExpiredPoints(@Param("userId") String userId, @Param("currentDate") Date currentDate);

    // 查询用户的总可用积分
    @Select("SELECT SUM(point) FROM Points WHERE userId = #{userId} AND valid = 1 AND date <= #{currentDate}")
    Integer getTotalAvailablePoints(@Param("userId") String userId, @Param("currentDate") Date currentDate);

    // 使用积分
    @Update("INSERT INTO Points(userId, point, date, valid) VALUES(#{userId}, #{pointsToUse}, #{currentDate}, 1)" )
    void usePoints(@Param("userId") String userId, @Param("pointsToUse") Integer pointsToUse, @Param("currentDate") Date currentDate);

    // 增加积分
    @Insert("INSERT INTO Points(userId, point, date, valid) VALUES(#{userId}, #{pointsToAdd}, #{currentDate}, 1)")
    void addPoints(@Param("userId") String userId, @Param("pointsToAdd") Integer pointsToAdd, @Param("currentDate") Date currentDate);

    // 更新过期积分
    @Update("UPDATE Points SET valid = 0 WHERE date > #{currentDate}")
    void updateExpiredPoints(@Param("currentDate") Date currentDate);
}